
import {
    baseUrl
} from '@/config/index.js'
import app from "@/main";

/**
 * 利用 Promise 封装网络请求 uni.request
 *
 * @param {Object} params 请求配置项
 * @param {Boolean} [disableLoading] 是否禁用加载提示(多个请求合并在一起时建议使用)
 * @returns Promise
 */
const request = (params, disableLoading) => {
    return new Promise((resolve, reject) => {
        const {
            url,
            method,
            data
        } = params;

        if (!disableLoading) {
            // 开启加载提示
            app.$showLoading();
        }

        uni.request({
            url: `${baseUrl}${url}`,
            method,
            data
        }).then(res => {
            const [error, data] = res;

            error ? reject(error) : resolve(data);

            if (!disableLoading) {
                // 关闭加载提示
                app.$hideLoading()
            }
        });
    })
}

export {
    request
}
